package com.delio.client.produccion.ingresos;

import java.util.ArrayList;
import java.util.List;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.produccion.documentos.Documento;
import com.delio.client.produccion.documentos.Documentos;
import com.delio.client.produccion.motivos.Motivo;
import com.delio.client.produccion.motivos.Motivos;
import com.delio.client.produccion.prioridades.Prioridad;
import com.delio.client.produccion.prioridades.Prioridades;
import com.delio.client.produccion.tipodocumentos.TipoDocumento;
import com.delio.client.produccion.tipodocumentos.TipoDocumentos;
import com.delio.client.produccion.unidades.Unidad;
import com.delio.client.produccion.unidades.Unidades;
import com.delio.client.produccion.tipos.Tipo;
import com.delio.client.produccion.tipos.Tipos;
import com.delio.client.util.ComboboxFieldBinding;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.custom.Portal;
import com.extjs.gxt.ui.client.widget.custom.Portlet;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
//import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;

public class IngresoForm extends LayoutContainer{
	private DelioConstants x = GWT.create(DelioConstants.class);
	EditorGrid<ModelData> gtd,gpr,gart,gund,gmot,gdoc;
	Button btnSaveTD,btnNewTD,btnDeleteTD,btnSavePR,btnNewPR,btnDeletePR,btnSaveART,btnNewART,btnDeleteART,btnSaveUND,btnNewUND,btnDeleteUND,btnSaveMOT,btnNewMOT,btnDeleteMOT,btnUpdateDOC,btnNewDOC,btnDeleteDOC;
	private BeanModel selectedModelTD,selectedModelPR,selectedModelUND,selectedModelART,selectedModelMOT,selectedModelDOC;
	TextField<String>  txtDescripcionTD,txtDescripcionPR,txtDescripcionART,txtDescripcionUND,txtDescripcionMOT,txtTipodocumentoMOT;
	ComboBox<BeanModel> cmbMOT;
	Dialog dl;
	FormPanel fp;
	TextField<String> numDoc;
	ComboBox<ModelData> tdoc;
	ComboBox<BeanModel> mot;
	HiddenField<String> id;
	DateField fecha;
	Integer idtdoc,idmot;
	FormBinding fbdoc;
	
	public IngresoForm(){
		
	    setLayout(new BorderLayout());  
	    LayoutContainer north = new LayoutContainer();  
	    BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 10);  
	  
	    ContentPanel west = new ContentPanel();
	   
	    west.setBodyBorder(false);  
	    west.setHeading(x.configuracion());  
	    west.setLayout(new AccordionLayout());  
	  
	    ContentPanel nav = new ContentPanel();  
	    nav.setHeading("Navigation");  
	    nav.setBorders(false);  
		nav.setBodyStyle("fontWeight:bold;padding:13px;");
	    //nav.setBodyStyle("fontSize: 12px; padding: 6px"); 
	    nav.add(new Button("Nuevo"));
	    nav.addText("HOllllllllla");  
	    west.add(nav);  
	  
	    ContentPanel settings = new ContentPanel();  
	    settings.setHeading("Settings");  
	    settings.setBorders(false);  
	    west.add(settings); 
	  
	    BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 700, 400, 500);  
	    westData.setMargins(new Margins(5, 0, 5, 5));  
	    westData.setCollapsible(true);   
	  
	    Portal portal = new Portal(2);  
	    portal.setBorders(true);  
	    portal.setStyleAttribute("backgroundColor", "SkyBlue");  
	    portal.setColumnWidth(0, .25);  
	    portal.setColumnWidth(1, .25);  
	    //portal.setColumnWidth(2, .25);  
	    //portal.setColumnWidth(3, .25);  
	    //portal.setColumnWidth(4, .30);  
	    //portal.setColumnWidth(5, .45);  
			      
	    Portlet portlet = new Portlet();  
	    TipoDocumentos.getStore();
	    portlet.setHeading("Tipo Documento");  
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridTD());
	    configPanelTD(portlet);
	    portlet.setHeight(200);  
	    portal.add(portlet, 0); 

	    portlet = new Portlet();  
		Prioridades.listarprioridades();
	    portlet.setHeading("Prioridades");
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridPR());
	    configPanelPR(portlet); 
	    portlet.setHeight(200);
	    portal.add(portlet, 1);  
	  
	    /*portlet = new Portlet();  
		Tipos.listartipos();
	    portlet.setHeading("Tipo Articulos"); 
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridART());
	    configPanelART(portlet);  
	    portlet.setHeight(200);
	    portal.add(portlet, 2);*/  
	  
	  /*  portlet = new Portlet();  
		Unidades.listarestadoUnidades();
	    portlet.setHeading("Unidades");  
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridUND());
	    configPanelUND(portlet);  
	    portlet.setHeight(200);
	    portal.add(portlet, 3);*/  
		  
	  /*  portlet = new Portlet();
	    Motivos.getStore();
	    TipoDocumentos.getStoreS();
	    portlet.setHeading("Motivos");  
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridMOT());
	    configPanelMOT(portlet); 
	    portlet.setHeight(200);
	    portal.add(portlet, 4);*/  
		  
	   /* portlet = new Portlet();
	    FormDOC();
	    Documentos.getStore();
	    portlet.setHeading("Documentos");  
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridDOC());
	    configPanelDOC(portlet); 
	    portlet.setHeight(200);
	    portal.add(portlet, 5);*/  
	  
	    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5));  
	  
	    add(north, northData);  
	    add(west, westData);  
	    add(portal, centerData);  
	  }  
	 
  public Grid<ModelData> createGridTD() {  
	 
	List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
	
	CheckBoxSelectionModel<ModelData> sm = new CheckBoxSelectionModel<ModelData>();
    conf.add(sm.getColumn());
    
    ColumnConfig col = new ColumnConfig("descripcion","Descripcion",200);
    
    txtDescripcionTD = new TextField<String>();  
    txtDescripcionTD.setAllowBlank(false);  
    col.setEditor(new CellEditor(txtDescripcionTD));  
    conf.add(col);  
  
    ColumnModel cm = new ColumnModel(conf); 
    
    gtd = new EditorGrid<ModelData>(TipoDocumentos.storeCombo, cm); 
    gtd.setLoadMask(true); 
    gtd.setBorders(true); 
    gtd.addPlugin(sm);

    gtd.setSelectionModel(sm);
    gtd.getSelectionModel().addListener(Events.SelectionChange,
       new Listener<SelectionChangedEvent<ModelData>>() {
		@Override
		public void handleEvent(SelectionChangedEvent<ModelData> be) {
		  if(be.getSelection().size()==0){
		      btnDeleteTD.setEnabled(false);
			  btnSaveTD.setEnabled(false);
			  btnNewTD.setEnabled(true);
          }
		  if(be.getSelection().size()==1){
			 // be.getSelection().get(0);
		      selectedModelTD = (BeanModel) be.getSelectedItem();
		      TipoDocumentos.tipodocumento_id = selectedModelTD.get("id");
		      System.out.println("el id del tipodocumento "+TipoDocumentos.tipodocumento_id);
              btnDeleteTD.setEnabled(true);
			  btnSaveTD.setEnabled(true);
			  btnNewTD.setEnabled(false);
	       }
 		}
    });	
    gtd.setBorders(true);  
    return gtd;  
  }    
	  
	private void configPanelTD(final ContentPanel panel) {  
		  panel.setCollapsible(true);  
		  panel.setAnimCollapse(false);  
		  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
		  panel.getHeader().addTool(  
		      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
		        @Override  
		         public void componentSelected(IconButtonEvent ce) {  
		           panel.removeFromParent();  
		         }  
		     }));  
		   panel.setTopComponent(toolbarTD());
	    }
		
	public ToolBar toolbarTD(){
		  ToolBar toolBar = new ToolBar(); 
		  btnNewTD = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
		  btnSaveTD = new Button("Guardar", IconHelper.createPath("images/icons/save_all.png"));
		  btnDeleteTD = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
		  btnSaveTD.setEnabled(false);
		  btnDeleteTD.setEnabled(false);
		  
		  toolBar.add(btnNewTD);
		  toolBar.add(btnSaveTD);
		  toolBar.add(btnDeleteTD);
	      
	      btnDeleteTD.addSelectionListener(new SelectionListener<ButtonEvent>(){
	       	public void componentSelected(ButtonEvent ce){
	       	  if(TipoDocumentos.tipodocumento_id != null){
	       		  System.out.println("tttttttttooooooooooodoooooooo");
				Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
				  public void handleEvent(MessageBoxEvent be) {
			        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
				       TipoDocumentos.eliminar(TipoDocumentos.tipodocumento_id);
				       TipoDocumentos.loaderCombo.load();
				      }
			        }
				  };
				    String msg = "Esta seguro que desea eliminar el Tipo Documentos?";
				    MessageBox.confirm(x.eliminarCliente(), msg, callback);
				  }else{
				  //TipoDocumentos.storeCombo.rejectChanges();//elimina los datos almacenados en memoria
				   TipoDocumentos.storeCombo.remove(0);
				}
	    	}
	    	});
	      btnNewTD.addSelectionListener(new SelectionListener<ButtonEvent>(){
	    	public void componentSelected(ButtonEvent ce){
	          TipoDocumento td = new TipoDocumento();
			   td.setDescripcion("");
			   BeanModelFactory beanModelFactory =  BeanModelLookup.get().getFactory(td.getClass());
			   gtd.stopEditing();
			
			   TipoDocumentos.storeCombo.insert(beanModelFactory.createModel(td), 0);
			   gtd.startEditing(TipoDocumentos.storeCombo.indexOf(beanModelFactory.createModel(td)), 0);
	    	}
	    	});
	      btnSaveTD.addSelectionListener(new SelectionListener<ButtonEvent>(){
	    	public void componentSelected(ButtonEvent ce){
	    	  TipoDocumento td = new TipoDocumento();
	 		   td.setDescripcion(txtDescripcionTD.getValue()); 
	 		   btnSaveTD.setEnabled(false);	  
	 		  if(TipoDocumentos.tipodocumento_id==null){
	 		   TipoDocumentos.guardar(td);
	 		   }
	 		  if(TipoDocumentos.tipodocumento_id!=null){
	 		   td.setId(TipoDocumentos.tipodocumento_id);
		 	   TipoDocumentos.actualizar(td);
	 		   }
	 		   TipoDocumentos.storeCombo.commitChanges();
	 		   TipoDocumentos.loaderCombo.load();	 		  
	    	}
	       });
	     return toolBar;
	    }
		
		
	  public Grid<ModelData> createGridPR() { 

	  	List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
		
		CheckBoxSelectionModel<ModelData> sm = new CheckBoxSelectionModel<ModelData>();
	    conf.add(sm.getColumn());
	    ColumnConfig col = new ColumnConfig("descripcion","Descripcion",200);
	    
	    txtDescripcionPR = new TextField<String>();  
	    txtDescripcionPR.setAllowBlank(false);  
	    col.setEditor(new CellEditor(txtDescripcionPR));  
	    conf.add(col);  
	  
	    ColumnModel cm = new ColumnModel(conf); 
	    
	    gpr = new EditorGrid<ModelData>(Prioridades.store, cm); 
	    gpr.setLoadMask(true); 
	    gpr.setBorders(true); 
	    gpr.addPlugin(sm);

	    gpr.setSelectionModel(sm);
	    gpr.getSelectionModel().addListener(Events.SelectionChange,
	       new Listener<SelectionChangedEvent<ModelData>>() {
			@Override
			public void handleEvent(SelectionChangedEvent<ModelData> be) {
			 if(be.getSelection().size()==0){
			      btnDeletePR.setEnabled(false);
				  btnSavePR.setEnabled(false);
				  btnNewPR.setEnabled(true);
	          }
			  if(be.getSelection().size()==1){
				 // be.getSelection().get(0);
			      selectedModelPR = (BeanModel) be.getSelectedItem();
			      Prioridades.prioridad_id = selectedModelPR.get("id");
			      System.out.println("el id del prioridad "+Prioridades.prioridad_id);
	              btnDeletePR.setEnabled(true);
				  btnSavePR.setEnabled(true);
				  btnNewPR.setEnabled(false);
		       }
	 		}
	    });	
	    gpr.setBorders(true);  
	    return gpr;  
	  }    	
		
	private void configPanelPR(final ContentPanel panel) {  
		  panel.setCollapsible(true);  
		  panel.setAnimCollapse(false);  
		  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
		  panel.getHeader().addTool(  
		      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
		        @Override  
		         public void componentSelected(IconButtonEvent ce) {  
		           panel.removeFromParent();  
		         }  
		     }));  
		   panel.setTopComponent(toolbarPR());
	    }
		
		public ToolBar toolbarPR(){
		  ToolBar toolBar = new ToolBar(); 
		  btnNewPR = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
		  btnSavePR = new Button("Guardar", IconHelper.createPath("images/icons/save_all.png"));
		  btnDeletePR = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
		  btnSavePR.setEnabled(false);
		  btnDeletePR.setEnabled(false);
		  
		  toolBar.add(btnNewPR);
		  toolBar.add(btnSavePR);
		  toolBar.add(btnDeletePR);
	      
	      btnDeletePR.addSelectionListener(new SelectionListener<ButtonEvent>(){
	       public void componentSelected(ButtonEvent ce){
	       	if(Prioridades.prioridad_id != null){
	       		System.out.println("tttttttttooooooooooodoooooooo");
				Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
				  public void handleEvent(MessageBoxEvent be) {
			        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
				       Prioridades.eliminar(Prioridades.prioridad_id);
				       Prioridades.loader.load();
				      }
			        }
				  };
				    String msg = "Esta seguro que desea eliminar esta Prioridad?";
				    MessageBox.confirm(x.eliminarCliente(), msg, callback);
				  }else{
				    Prioridades.store.remove(0);
				}
	       	   }
	    	 });
	      btnNewPR.addSelectionListener(new SelectionListener<ButtonEvent>(){
	    	public void componentSelected(ButtonEvent ce){
	    	    Prioridad pr = new Prioridad();
				pr.setDescripcion("");
				BeanModelFactory beanModelFactory =  BeanModelLookup.get().getFactory(pr.getClass());
				gpr.stopEditing();
			
			    Prioridades.store.insert(beanModelFactory.createModel(pr), 0);
				gpr.startEditing(Prioridades.store.indexOf(beanModelFactory.createModel(pr)), 0);
	    	 }
	    	});
	        btnSavePR.addSelectionListener(new SelectionListener<ButtonEvent>(){
	    	public void componentSelected(ButtonEvent ce){
	    		Prioridad  pr = new Prioridad();
		 		pr.setDescripcion(txtDescripcionPR.getValue()); 
		 		btnSavePR.setEnabled(false);	  
		 		if(Prioridades.prioridad_id==null){
		 		 Prioridades.guardar(pr);
		 		}
		 		if(Prioridades.prioridad_id!=null){
		 		 pr.setId(Prioridades.prioridad_id);
			 	 Prioridades.actualizar(pr);
		 		}
		 		 Prioridades.store.commitChanges();
		 		 Prioridades.loader.load();	 		
	    	}
	       });
	     return toolBar;
	    }
	
	 /* public Grid<ModelData> createGridART() {  

		  	List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
			
			CheckBoxSelectionModel<ModelData> sm = new CheckBoxSelectionModel<ModelData>();
		    conf.add(sm.getColumn());
		    
		    ColumnConfig col = new ColumnConfig("descripcion","Descripcion",200);
		    
		    txtDescripcionART = new TextField<String>();  
		    txtDescripcionART.setAllowBlank(false);  
		    col.setEditor(new CellEditor(txtDescripcionART));  
		    conf.add(col);  
		  
		    ColumnModel cm = new ColumnModel(conf); 
		    
		    gart = new EditorGrid<ModelData>(Tipos.store, cm); 
		    gart.setLoadMask(true); 
		    gart.setBorders(true); 
		    gart.addPlugin(sm);

		    gart.setSelectionModel(sm);
		    gart.getSelectionModel().addListener(Events.SelectionChange,
		       new Listener<SelectionChangedEvent<ModelData>>() {
				@Override
				public void handleEvent(SelectionChangedEvent<ModelData> be) {
				  if(be.getSelection().size()==0){
					btnDeleteART.setEnabled(false);
					btnSaveART.setEnabled(false);
					btnNewART.setEnabled(true);
			       }
				  if(be.getSelection().size()==1){
				 // be.getSelection().get(0);
				    selectedModelART = (BeanModel) be.getSelectedItem();
				    Tipos.tipo_id = selectedModelART.get("id");
				    System.out.println("el id del tipo "+Tipos.tipo_id);
			        btnDeleteART.setEnabled(true);
				    btnSaveART.setEnabled(true);
					btnNewART.setEnabled(false);
				  }
		 		}
		    });	
		    gart.setBorders(true);  
		    return gart;  
		  }    	
			
		private void configPanelART(final ContentPanel panel) {  
			  panel.setCollapsible(true);  
			  panel.setAnimCollapse(false);  
			  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
			  panel.getHeader().addTool(  
			      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
			        @Override  
			         public void componentSelected(IconButtonEvent ce) {  
			           panel.removeFromParent();  
			         }  
			     }));  
			   panel.setTopComponent(toolbarART());
		    }
			
			public ToolBar toolbarART(){
			  ToolBar toolBar = new ToolBar(); 
			  btnNewART = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
			  btnSaveART = new Button("Guardar", IconHelper.createPath("images/icons/save_all.png"));
			  btnDeleteART = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
			  btnSaveART.setEnabled(false);
			  btnDeleteART.setEnabled(false);
			  
			  toolBar.add(btnNewART);
			  toolBar.add(btnSaveART);
			  toolBar.add(btnDeleteART);
		      
		      btnDeleteART.addSelectionListener(new SelectionListener<ButtonEvent>(){
		       	public void componentSelected(ButtonEvent ce){
	       		if(Tipos.tipo_id != null){
		       		System.out.println("tttttttttooooooooooodoooooooo");
					Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
					  public void handleEvent(MessageBoxEvent be) {
				        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
					       Tipos.eliminar(Tipos.tipo_id);
					       Tipos.loader.load();
					      }
				        }
					  };
					    String msg = "Esta seguro que desea eliminar este Articulos?";
					    MessageBox.confirm(x.eliminarCliente(), msg, callback);
					  }else{
					    Tipos.store.remove(0);
					}
		       	   }
		    	});
		      btnNewART.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    	    Tipo t = new Tipo();
					t.setDescripcion("");
					BeanModelFactory beanModelFactory =  BeanModelLookup.get().getFactory(t.getClass());
					gart.stopEditing();
				
					Tipos.store.insert(beanModelFactory.createModel(t), 0);
					gart.startEditing(Tipos.store.indexOf(beanModelFactory.createModel(t)), 0);
		    	}
		    	});
		        btnSaveART.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    		Tipo  t = new Tipo();
			 		t.setDescripcion(txtDescripcionART.getValue()); 
			 		btnSaveART.setEnabled(false);	  
			 		if(Tipos.tipo_id==null){
			 		  Tipos.guardar(t);
			 		}
			 		if(Tipos.tipo_id!=null){
			 		  t.setId(Tipos.tipo_id);
				 	  Tipos.actualizar(t);
			 		}
			 		 Tipos.store.commitChanges();
			 		 Tipos.loader.load();
		    	}
		       });
		     return toolBar;
		    }	

			
	  public Grid<ModelData> createGridUND() {  
		
		  
			List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
			
			CheckBoxSelectionModel<ModelData> sm = new CheckBoxSelectionModel<ModelData>();
		    conf.add(sm.getColumn());
		  
		    ColumnConfig col = new ColumnConfig("descripcion","Descripcion",200);
			    
		    txtDescripcionUND = new TextField<String>();  
		    txtDescripcionUND.setAllowBlank(false);  
		    col.setEditor(new CellEditor(txtDescripcionUND));  
		    conf.add(col);  
		  
		    ColumnModel cm = new ColumnModel(conf); 
		    
		    gund = new EditorGrid<ModelData>(Unidades.store, cm); 
		    gund.setLoadMask(true); 
		    gund.setBorders(true); 
		    gund.addPlugin(sm);

		    gund.setSelectionModel(sm);
		    gund.getSelectionModel().addListener(Events.SelectionChange,
		       new Listener<SelectionChangedEvent<ModelData>>() {
				@Override
				public void handleEvent(SelectionChangedEvent<ModelData> be) {
			      if(be.getSelection().size()==0){
				      btnDeleteUND.setEnabled(false);
					  btnSaveUND.setEnabled(false);
					  btnNewUND.setEnabled(true);
		           }
				  if(be.getSelection().size()==1){
					 // be.getSelection().get(0);
				      selectedModelUND = (BeanModel) be.getSelectedItem();
				      Unidades.unidad_id = selectedModelUND.get("id");
				      System.out.println("el id de la unidad "+Unidades.unidad_id);
		              btnDeleteUND.setEnabled(true);
					  btnSaveUND.setEnabled(true);
					  btnNewUND.setEnabled(false);
			       }
		 		}
		    });	
		    gund.setBorders(true);  
		    return gund;  
		  }    	
			
		private void configPanelUND(final ContentPanel panel) {  
			  panel.setCollapsible(true);  
			  panel.setAnimCollapse(false);  
			  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
			  panel.getHeader().addTool(  
			      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
			        @Override  
			         public void componentSelected(IconButtonEvent ce) {  
			           panel.removeFromParent();  
			         }  
			     }));  
			   panel.setTopComponent(toolbarUND());
		    }
			
		public ToolBar toolbarUND(){
			  ToolBar toolBar = new ToolBar(); 
			  btnNewUND = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
			  btnSaveUND = new Button("Guardar", IconHelper.createPath("images/icons/save_all.png"));
			  btnDeleteUND = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
			  btnSaveUND.setEnabled(false);
			  btnDeleteUND.setEnabled(false);
			  
			  
			  toolBar.add(btnNewUND);
			  toolBar.add(btnSaveUND);
			  toolBar.add(btnDeleteUND);
		      
		      btnDeleteUND.addSelectionListener(new SelectionListener<ButtonEvent>(){
		       	public void componentSelected(ButtonEvent ce){
	       		 if(Unidades.unidad_id != null){
		       		 System.out.println("tttttttttooooooooooodoooooooo");
					 Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
					  public void handleEvent(MessageBoxEvent be) {
				        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
					       Unidades.eliminar(Unidades.unidad_id);
					       Unidades.loader.load();
					      }
				        }
					  };
					    String msg = "Esta seguro que desea eliminar el Tipo Documentos?";
					    MessageBox.confirm(x.eliminarCliente(), msg, callback);
					  }else{
					  //TipoDocumentos.storeCombo.rejectChanges();//elimina los datos almacenados en memoria
					   Unidades.store.remove(0);
					}
	        	}
		    	});
		      btnNewUND.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    		Unidad und = new Unidad();
					und.setDescripcion("");
					BeanModelFactory beanModelFactory =  BeanModelLookup.get().getFactory(und.getClass());
					gund.stopEditing();
				
					Unidades.store.insert(beanModelFactory.createModel(und), 0);
					gund.startEditing(Unidades.store.indexOf(beanModelFactory.createModel(und)), 0);
			  	 }
		    	});
		        btnSaveUND.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
	    		   Unidad und = new Unidad();
	    		   und.setDescripcion(txtDescripcionUND.getValue()); 
		 		   btnSaveUND.setEnabled(false);	  
		 		  if(Unidades.unidad_id==null){
		 		   Unidades.guardar(und);
		 		   }
		 		  if(Unidades.unidad_id!=null){
		 		   und.setId(Unidades.unidad_id);
			 	   Unidades.actualizar(und);
		 		   }
		 		   Unidades.store.commitChanges();
		 		   Unidades.loader.load();	
	    	     }
		       });
		     return toolBar;
		    }*/

	/*	  public EditorGrid<ModelData> createGridMOT() {  
			  
				List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
				
				CheckBoxSelectionModel<ModelData> sm = new CheckBoxSelectionModel<ModelData>();
			    conf.add(sm.getColumn());
			    
			   //ColumnConfig col = new ColumnConfig("tipodocumento_id","Tipo Documento",100);
			    /*txtCodigoUND = new TextField<String>();  
			    txtCodigoUND.setAllowBlank(false);  
			    col.setEditor(new CellEditor(txtCodigoUND));  
			    conf.add(col);*/  

//////////////////////////////////////////////////////
			   // Motivos.loaderCombo.load();
			   // TipoDocumentos.loaderComboS.load();
  /*  ////cerrar       
                TipoDocumentos.getStoreS();
			    final SimpleComboBox<TipoDocumento> cmbMOT = new SimpleComboBox<TipoDocumento>();
				cmbMOT.setAllowBlank(true);
				cmbMOT.setVisible(true);
				cmbMOT.setForceSelection(true);
				cmbMOT.setEditable(true);
				cmbMOT.setEmptyText("Seleccione...");
				cmbMOT.setDisplayField("descripcion");
				cmbMOT.setStore(TipoDocumentos.storeComboS);
				cmbMOT.setTriggerAction(TriggerAction.ALL);
				cmbMOT.setValueField("descripcion");
				cmbMOT.setUseQueryCache(false);

				CellEditor editor = new CellEditor(cmbMOT) {
					@Override
					public Object preProcessValue(Object value) {
					  if (value == null) {
						 return value;
					  }
					   return cmbMOT.getStore().findModel(value.toString());

					}
					
				//	@Override
					public Object postProcessValue(Object value) {
						if (value == null) {
							return value;
						}
						Integer id = ((BeanModel) value).get("id");
						gmot.getSelectionModel().getSelectedItem().set("id", id);
							//.setId(id);
						return ((BeanModel) value).get("descripcion");

					}
				};
		        ColumnConfig col = new ColumnConfig();  
		        //col.setId("id");  
		        col.setHeader("Tipo Documento");  
		        col.setWidth(130);  
		        col.setEditor(editor);  
		        conf.add(col);
	/////cerrar	      
			///////el combo esta sin el store...   
/////////////////////////////////////////////////////////
//			    ColumnConfig col = new ColumnConfig();
//			    CellEditor editor = new CellEditor(new ComboBox<BeanModel>());
//	            editor.setSwallowKeys(false);
//	            col.setEditor(editor);
//	            conf.add(col);
//	    			    
/////////////////////////////////////////////////////////			    
			  final SimpleComboBox<String> cmbMOT = new SimpleComboBox<String>();  
			  cmbMOT.setForceSelection(true);  
			  cmbMOT.setTriggerAction(TriggerAction.ALL);  
			  cmbMOT.add("Boleta");  
			  cmbMOT.add("Factura");  
			  cmbMOT.add("Pedidos");  
			  cmbMOT.add("Otros");  
			  			  
			    CellEditor editor = new CellEditor(cmbMOT) {  
			      @Override  
			      public Object preProcessValue(Object value) {  
			        if (value == null) {  
			          return value;  
			        }  
			        return cmbMOT.findModel(value.toString());  
			      }  
			  
			      @Override  
			      public Object postProcessValue(Object value) {  
			        if (value == null) {  
			          return value;  
			        }  
			        return ((ModelData) value).get("value");  
			      }  
			    };  
			  
			    ColumnConfig col = new ColumnConfig();  
			    col.setId("light");  
			    col.setHeader("Tipo Documentos");  
			    col.setWidth(130);  
			    col.setEditor(editor);  
			    conf.add(col);  	    
			  
			    col = new ColumnConfig("descripcion","Descripcion",200);
				    
			    txtDescripcionMOT = new TextField<String>();  
			    txtDescripcionMOT.setAllowBlank(false);  
			    col.setEditor(new CellEditor(txtDescripcionMOT));  
			    conf.add(col);  
			  
			    ColumnModel cm = new ColumnModel(conf); 
			    
			    gmot = new EditorGrid<ModelData>(Motivos.storeCombo, cm); 
			    gmot.setLoadMask(true); 
			    gmot.setBorders(true); 
			    gmot.addPlugin(sm);

			    gmot.setSelectionModel(sm);
			    gmot.getSelectionModel().addListener(Events.SelectionChange,
			       new Listener<SelectionChangedEvent<ModelData>>() {
					@Override
					public void handleEvent(SelectionChangedEvent<ModelData> be) {
				      if(be.getSelection().size()==0){
					      btnDeleteMOT.setEnabled(false);
						  btnSaveMOT.setEnabled(false);
						  btnNewMOT.setEnabled(true);
			           }
					  if(be.getSelection().size()==1){
						 // be.getSelection().get(0);
					      selectedModelMOT = (BeanModel) be.getSelectedItem();
					      Motivos.motivo_id = selectedModelMOT.get("id");
					      System.out.println("el id del motivo "+Motivos.motivo_id);
			              btnDeleteMOT.setEnabled(true);
						  btnSaveMOT.setEnabled(true);
						  btnNewMOT.setEnabled(false);
				       }
			 		}
			    });	
			    gmot.setBorders(true);  
			    return gmot;  
			  }    	
				
			private void configPanelMOT(final ContentPanel panel) {  
				  panel.setCollapsible(true);  
				  panel.setAnimCollapse(false);  
				  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
				  panel.getHeader().addTool(  
				      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
				        @Override  
				         public void componentSelected(IconButtonEvent ce) {  
				           panel.removeFromParent();  
				         }  
				     }));  
				   panel.setTopComponent(toolbarMOT());
			    }
				
				public ToolBar toolbarMOT(){
				  ToolBar toolBar = new ToolBar(); 
				  btnNewMOT = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
				  btnSaveMOT = new Button("Guardar", IconHelper.createPath("images/icons/save_all.png"));
				  btnDeleteMOT = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
				  btnSaveMOT.setEnabled(false);
				  btnDeleteMOT.setEnabled(false);
				  
				  
				  toolBar.add(btnNewMOT);
				  toolBar.add(btnSaveMOT);
				  toolBar.add(btnDeleteMOT);
			      
			      btnDeleteMOT.addSelectionListener(new SelectionListener<ButtonEvent>(){
			       	public void componentSelected(ButtonEvent ce){
		       		 if(Motivos.motivo_id != null){
			       		 System.out.println("tttttttttooooooooooodoooooooo");
						 Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
						  public void handleEvent(MessageBoxEvent be) {
					        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
						       Motivos.eliminar(Motivos.motivo_id);
						       Motivos.loaderCombo.load();
						      }
					        }
						  };
						    String msg = "Esta seguro que desea eliminar el Tipo Documentos?";
						    MessageBox.confirm(x.eliminarCliente(), msg, callback);
						  }else{
						  //TipoDocumentos.storeCombo.rejectChanges();//elimina los datos almacenados en memoria
						   Motivos.storeCombo.remove(0);
						}
		        	}
			    	});
			      btnNewMOT.addSelectionListener(new SelectionListener<ButtonEvent>(){
			    	public void componentSelected(ButtonEvent ce){
			    		Motivo mot = new Motivo();
						mot.setDescripcion("");
						mot.setTddescripcion("");
						BeanModelFactory beanModelFactory =  BeanModelLookup.get().getFactory(mot.getClass());
						gmot.stopEditing();
					
						Motivos.storeCombo.insert(beanModelFactory.createModel(mot), 0);
						gmot.startEditing(Motivos.storeCombo.indexOf(beanModelFactory.createModel(mot)), 0);
				  	 }
			    	});
			        btnSaveMOT.addSelectionListener(new SelectionListener<ButtonEvent>(){
			    	public void componentSelected(ButtonEvent ce){
		    		   Motivo mot = new Motivo();
		    		   mot.setDescripcion(cmbMOT.getValue().toString());
			 		   mot.setTddescripcion(txtDescripcionMOT.getValue()); 
			 		   btnSaveMOT.setEnabled(false);	  
			 		  if(Motivos.motivo_id==null){
			 		   Motivos.guardar(mot);
			 		   }
			 		  if(Motivos.motivo_id!=null){
			 		   mot.setId(Motivos.motivo_id);
				 	   Motivos.actualizar(mot);
			 		   }
			 		   Motivos.storeCombo.commitChanges();
			 		   Motivos.loaderCombo.load();	
		    	     }
			       });
			     return toolBar;
			    }
///////////////////////////////////////////////////////////////////////////////				
	/*	 public Grid<ModelData> createGridDOC() {  
					
		  
			List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
			
			final CheckBoxSelectionModel<ModelData> smdoc = new CheckBoxSelectionModel<ModelData>();
		    conf.add(smdoc.getColumn());
		    
		    ColumnConfig col = new ColumnConfig("numero","Codigo",100);
		    conf.add(col);  
		    col = new ColumnConfig("tipdescripcion","Tipo Documento",100);
			conf.add(col);  
		    col = new ColumnConfig("motdescripcion","Motivo",100);
			conf.add(col);  
			col = new ColumnConfig("fecha","Fecha",100);
		    conf.add(col);  
		 
		    ColumnModel cm = new ColumnModel(conf); 
		    
		    gdoc = new EditorGrid<ModelData>(Documentos.storeCombo, cm); 
		    gdoc.setLoadMask(true); 
		    gdoc.setBorders(true); 
		    gdoc.addPlugin(smdoc);

		    smdoc.setSelectionMode(SelectionMode.SINGLE);
		    gdoc.setSelectionModel(smdoc);
		    gdoc.getSelectionModel().addListener(Events.SelectionChange,
		       new Listener<SelectionChangedEvent<ModelData>>() {
				@Override
				public void handleEvent(SelectionChangedEvent<ModelData> be) {
			      if(be.getSelection().size()==0){
				      btnDeleteDOC.setEnabled(false);
					  btnUpdateDOC.setEnabled(false);
					  btnNewDOC.setEnabled(true);
		           }
				  if(be.getSelection().size()==1){
					 // be.getSelection().get(0);
					  //fbdoc.bind((BeanModel) be.getSelection().get(0));
				      selectedModelDOC = (BeanModel) be.getSelectedItem();
				      Documentos.documento_id = selectedModelDOC.get("id");
				      System.out.println("el id del documento "+Documentos.documento_id);
		              btnDeleteDOC.setEnabled(true);
					  btnUpdateDOC.setEnabled(true);
					  btnNewDOC.setEnabled(false);
			       }
		 		}
		    });	
		    gdoc.setBorders(true);  
		  /////////////////////  
		    gdoc.setLoadMask(true);  
	                              
	        fbdoc = new FormBinding(fp);
	        fbdoc.setStore(Documentos.storeCombo);
	        fbdoc.addFieldBinding(new FieldBinding(id,"id"));
	        fbdoc.addFieldBinding(new FieldBinding(numDoc,"numero"));
	        fbdoc.addFieldBinding(new ComboboxFieldBinding(tdoc,"tipdescripcion"));
		    fbdoc.addFieldBinding(new ComboboxFieldBinding(mot,"motdescripcion"));
		    fbdoc.addFieldBinding(new FieldBinding(fecha,"fecha"));		    
		 //////////////////////////////////////////   
		    return gdoc;  
		  }    	
			
		private void configPanelDOC(final ContentPanel panel) {  
			  panel.setCollapsible(true);  
			  panel.setAnimCollapse(false);  
			  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
			  panel.getHeader().addTool(  
			      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
			        @Override  
			         public void componentSelected(IconButtonEvent ce) {  
			           panel.removeFromParent();  
			         }  
			     }));  
			   panel.setTopComponent(toolbarDOC());
		    }
			
		public ToolBar toolbarDOC(){
			  ToolBar toolBar = new ToolBar(); 
			  btnNewDOC = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
			  btnUpdateDOC = new Button("Actualizar", IconHelper.createPath("images/icons/update.png"));
			  btnDeleteDOC = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
			  btnUpdateDOC.setEnabled(false);
			  btnDeleteDOC.setEnabled(false);
			  
			  
			  toolBar.add(btnNewDOC);
			  toolBar.add(btnUpdateDOC);
			  toolBar.add(btnDeleteDOC);
//////////////////////////////////////////// 
			  dl = new Dialog();  
			  dl.setBodyBorder(false);  
			  dl.setHeading("Nuevo Documento");  
			  dl.setWidth(350);  
			  dl.setHeight(200);
			  dl.setIconStyle("images/icons/new.gif");
			  dl.setModal(true);
				   
			  Button salirDOC   = new Button("Salir",IconHelper.create("images/icons/exit.png"));
			  Button guardarDOC = new Button("Guardar",IconHelper.create("images/icons/save_all.png"));
				  
			  dl.setButtons("");
			  dl.addButton(salirDOC);
			  dl.addButton(guardarDOC);
		      dl.setFrame(true);
		      
		      guardarDOC.addSelectionListener(new SelectionListener<ButtonEvent>(){
		       	public void componentSelected(ButtonEvent ce){
		       	   Documento doc = new Documento();
		       	    doc.setNumero(numDoc.getValue()); 
		       	    doc.setTipodocumento_id(idtdoc);
		       	    doc.setMotivo_id(idmot);
		       	    doc.setFecha(fecha.getValue());
		       	   
	       		    btnUpdateDOC.setEnabled(false);	  
		            if(id.getValue()==null){
		              Documentos.guardar(doc);
		            }
		            if(id.getValue()!=null){
		              doc.setId(Integer.parseInt(id.getValue().toString()));
				      Documentos.actualizar(doc);
				    }
		           dl.hide();
		           
		           Documentos.loaderCombo.load();
	 		   	}
		      });
		      
		      salirDOC.addSelectionListener(new SelectionListener<ButtonEvent>(){
			     public void componentSelected(ButtonEvent ce){
			     	dl.hide();
		       	 }
			   });

		      btnDeleteDOC.addSelectionListener(new SelectionListener<ButtonEvent>(){
		       	public void componentSelected(ButtonEvent ce){
	       		 if(Documentos.documento_id != null){
		       		 System.out.println("tttttttttooooooooooodoooooooo");
					 Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
					  public void handleEvent(MessageBoxEvent be) {
				        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
					       Documentos.eliminar(Documentos.documento_id);
					       Documentos.loaderCombo.load();
					      }
				        }
					  };
					    String msg = "Esta seguro que desea eliminar el Tipo Documentos?";
					    MessageBox.confirm(x.eliminarCliente(), msg, callback);
					  }else{
					  //TipoDocumentos.storeCombo.rejectChanges();//elimina los datos almacenados en memoria
					   Documentos.storeCombo.remove(0);
					}
	        	}
		    	});
		      btnNewDOC.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    	   clearForm(); 
		    	   dl.add(fp);
		    	   dl.show();
		    	   //fbdoc.unbind();
			  	  }
		    	});
		      
		      btnUpdateDOC.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    	   fbdoc.bind(selectedModelDOC);
		    	   dl.add(fp);
     	    	   dl.show();	
	  /* ////cerrar   	    	   
	    		   Documento doc = new Documento();
	    		   //und.setCodigo(txtCodigoUND.getValue());
		 		   //und.setDescripcion(txtDescripcionUND.getValue()); 
		 		   btnUpdateDOC.setEnabled(false);	  
		 		  if(Documentos.documento_id==null){
		 		   Documentos.guardar(doc);
		 		   }
		 		  if(Documentos.documento_id!=null){
		 		   doc.setId(Documentos.documento_id);
			 	   Documentos.actualizar(doc);
		 		   }
		 		   Documentos.storeCombo.commitChanges();
		 		   Documentos.loaderCombo.load();
		 ///////cerrar
	    	     }
		       });
		     return toolBar;
		}
		
		public void FormDOC(){
			fp = new FormPanel();
			fp.setHeaderVisible(false);
			fp.setFrame(true);
			id = new HiddenField<String>();
			id.setVisible(false);
			numDoc = new TextField<String>();
			numDoc.setFieldLabel("Nro Pedido");
			numDoc.setEnabled(false);
			tdoc = new ComboBox<ModelData>();  
			tdoc.setEmptyText("Select a state...");  
			tdoc.setAllowBlank(false);
			tdoc.setDisplayField("descripcion");  
			tdoc.setFieldLabel("Tipo Documento");
			tdoc.setWidth(150);  
			tdoc.setStore(TipoDocumentos.storeCombo);
			tdoc.setValueField("descripcion");
			tdoc.setTypeAhead(true);  
			tdoc.setTriggerAction(TriggerAction.ALL); 
			tdoc.addSelectionChangedListener(new SelectionChangedListener<ModelData>(){
			  public void selectionChanged(SelectionChangedEvent<ModelData> se) {
				 if(tdoc.getValue()!=null){   
					   idtdoc = Integer.parseInt(tdoc.getValue().get("id").toString());
				 }
			   }
			});
			mot = new ComboBox<BeanModel>();  
			mot.setEmptyText("Select a state...");  
			mot.setAllowBlank(false);
			mot.setDisplayField("descripcion");  
			mot.setFieldLabel("Motivo");
			mot.setWidth(150);  
		    mot.setStore(Motivos.storeCombo);
			mot.setValueField("descripcion");
			mot.setTypeAhead(true);  
			mot.setTriggerAction(TriggerAction.ALL); 
			mot.addSelectionChangedListener(new SelectionChangedListener<BeanModel>(){
			  public void selectionChanged(SelectionChangedEvent<BeanModel> se) {
				if(mot.getValue()!=null){   
				   idmot = Integer.parseInt(mot.getValue().get("id").toString());
				 }
			   }
			});	
			fecha = new DateField();
			fecha.setName("fecha");
			fecha.getPropertyEditor().setFormat(DateTimeFormat.getFormat("dd/MM/yyyy"));
			fecha.setAllowBlank(false);
			fecha.setFieldLabel("Fecha");

			fp.add(id);	
			fp.add(numDoc);
			fp.add(tdoc);
			fp.add(mot);
			fp.add(fecha);
		  //  fp.add(buttonForm());
		}
     public void clearForm(){
    	 fp.clear();
     }*/
}